package com.fastloan.mgt.jx.controller;

import com.fastloan.common.model.CheckResult;
import com.fastloan.common.model.CheckResultEnum;
import com.fastloan.core.apiauth.anno.DecryptParam;
import com.fastloan.core.apiauth.anno.Encrypt;
import com.fastloan.mgt.jx.model.dto.JxCreditDTO;
import com.fastloan.mgt.jx.model.dto.JxLoanDTO;
import com.fastloan.mgt.jx.model.dto.JxRepaymentDTO;
import com.fastloan.mgt.jx.model.dto.JxSeCreditDTO;
import com.fastloan.mgt.jx.model.vo.JxModelResultVO;
import com.fastloan.mgt.jx.service.JxService;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import com.kawins.spring.ContextProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 极鲜网调用接口
 *
 * @author chenkai
 * createTime 2018-01-2018/1/5 10:36
 */
@RestController
@RequestMapping("/product/jixian/anon")
public class JxCreditController extends BaseController<JxCreditDTO> {
    @Autowired
    private JxService jxService;

    /**
     * 预授信请求信息
     *
     * @return
     */
    @Encrypt
    @PostMapping("/credit-request")
    public Object preliminaryCredit(@DecryptParam JxCreditDTO jxCreditDTO) {
        try {
            String urlPath = ContextProperty.getContextProperty("jxUrlPath.url").toString();
            CheckResult checkResult = jxService.preliminaryCredit(jxCreditDTO);
            if (!checkResult.isCheckResult()) {
                return new ResultTo(checkResult.getCode(), checkResult.getCheckMsg());
            }
            return new ResultTo(CheckResultEnum.SUCCESS.getCode(), CheckResultEnum.SUCCESS.getMsg()).setData(urlPath + checkResult.getData());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultTo(CheckResultEnum.INTERNAL_ERROR.getCode(), CheckResultEnum.INTERNAL_ERROR.getMsg());
        }
    }

    /**
     * 预授信查询请求JSON格式
     *
     * @param jxSeCreditDTO
     * @return
     */
    @Encrypt
    @PostMapping("/credit-json")
    public Object resultCreditJson(@DecryptParam JxSeCreditDTO jxSeCreditDTO) {
        try {
            JxModelResultVO jxModelResultVO = jxService.backModelResult(jxSeCreditDTO);
            return new ResultTo(CheckResultEnum.SUCCESS.getCode(), CheckResultEnum.SUCCESS.getMsg()).setData(jxModelResultVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultTo(CheckResultEnum.INTERNAL_ERROR.getCode(), CheckResultEnum.INTERNAL_ERROR.getMsg());
        }
    }

    /**
     * 预授信查询请求
     *
     * @param jxSeCreditDTO
     * @return
     */
    @Encrypt
    @PostMapping("/credit")
    public Object resultCredit(@DecryptParam JxSeCreditDTO jxSeCreditDTO) {
        try {
            String urlPath = ContextProperty.getContextProperty("jxUrlPath.url").toString();
            CheckResult checkResult = jxService.queryAppIdByCardId(jxSeCreditDTO);
            if (!checkResult.isCheckResult()) {
                return new ResultTo(checkResult.getCode(), checkResult.getCheckMsg());
            }
            return new ResultTo(CheckResultEnum.SUCCESS.getCode(), CheckResultEnum.SUCCESS.getMsg()).setData(urlPath + checkResult.getData());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultTo(CheckResultEnum.INTERNAL_ERROR.getCode(), CheckResultEnum.INTERNAL_ERROR.getMsg());
        }
    }

    /**
     * 贷款请求信息
     *
     * @param jxLoanDTO
     * @return
     */
    @Encrypt
    @PostMapping("/loan-request")
    public Object insertApplication(@DecryptParam JxLoanDTO jxLoanDTO) {
        try {
            String urlPath = ContextProperty.getContextProperty("jxUrlPath.url").toString();
            CheckResult checkResult = jxService.applyApplication(jxLoanDTO);
            if (!checkResult.isCheckResult()) {
                return new ResultTo(checkResult.getCode(), checkResult.getCheckMsg());
            }
            return new ResultTo(CheckResultEnum.SUCCESS.getCode(), CheckResultEnum.SUCCESS.getMsg()).setData(urlPath + checkResult.getData());
        } catch (Exception e) {
            return new ResultTo(CheckResultEnum.INTERNAL_ERROR.getCode(), CheckResultEnum.INTERNAL_ERROR.getMsg());
        }
    }

    /**
     * 法人贷款（还款）情况查询
     *
     * @param jxRepaymentDTO
     * @return
     */
    @Encrypt
    @PostMapping("/loan-status")
    public Object queryRepayment(@DecryptParam JxRepaymentDTO jxRepaymentDTO) {
        try {
            String urlPath = ContextProperty.getContextProperty("jxUrlPath.url").toString();
            return new ResultTo(0, "success").setData(urlPath + "/#/fresh/loan/info/");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("请求失败, 请求参数:{}, 错误原因:{}", jxRepaymentDTO.toString(), e.getMessage());
            return new ResultTo(ResultEnum.OPERATION_FAILED, "请求失败");
        }
    }

}
